<template>
  <div style="display: flex">
    <div style="margin-right: 20px">
      <el-time-select
        style="width: 240px"
        v-model="startTime"
        :max-time="endTime"
        :placeholder="startPlaceholder"
        :start="startTimeStart"
        :step="startStep"
        :end="startTimeEnd"
        v-bind="$attrs.startOptions"
      />
    </div>
    <div>
      <el-time-select
        style="width: 240px"
        v-model="endTime"
        :min-time="startTime"
        :placeholder="endPlaceholder"
        :start="endTimeStart"
        :step="endStep"
        :end="endTimeEnd"
        :disabled="endTimeDisabled"
        v-bind="$attrs.endOptions"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from "vue";

let props = defineProps({
  startPlaceholder: {
    type: String,
    default: "请选择开始时间",
  },
  startTimeStart: {
    type: String,
    default: "08:00",
  },
  startStep: {
    type: String,
    default: "00:30",
  },
  startTimeEnd: {
    type: String,
    default: "24:00",
  },

  endPlaceholder: {
    type: String,
    default: "请选择结束时间",
  },
  endTimeStart: {
    type: String,
    default: "08:00",
  },
  endStep: {
    type: String,
    default: "00:30",
  },
  endTimeEnd: {
    type: String,
    default: "24:00",
  },
});

let emits = defineEmits(["startChange", "endChange"]);

let startTime = ref<string>("");
let endTime = ref<string>("");

// 是否禁用结束时间
let endTimeDisabled = ref<boolean>(true);

// 监听开始时间的变化
watch(
  () => startTime.value,
  (val) => {
    if (!val) {
      endTime.value = "";
      endTimeDisabled.value = true;
    } else {
      endTimeDisabled.value = false;
      // 给父组件分发事件
      emits("startChange", val);
    }
  }
);

watch(
  () => endTime.value,
  (val) => {
    if (val) {
      emits("endChange", {
        startTime: startTime.value,
        endTime: endTime.value,
      });
    }
  }
);


</script>

<style lang="scss" scoped></style>
